#include "response.h"
#include "string.h"
#include <string>
http_respones::http_respones()
{
}

http_respones::http_respones(int status,
                             const char *status_info, const char *content,
                             http_head &head, http_cookie &cookie)
{
    m_status = status;
    m_status_info = status_info;
    m_content = content;
    m_head = head;
    m_cookie = cookie;
}

const char *http_respones::data()
{
    //转换成http协议
    std::string data;

    char sz[1024] = {0x0};
    sprintf(sz, "HTTP/1.1 %d %s\n", m_status, m_status_info);
    data.append(sz);

    auto ite_head = m_head.begin();
    for (; ite_head != m_head.end(); ite_head++)
    {
        sprintf(sz, "%s: %s\n", ite_head->first.c_str(), ite_head->second.c_str());
        data.append(sz);
    }

    //如果content有数据
    size_t content_len = strlen(m_content);
    if (content_len > 0)
    {
        sprintf(sz, "%s: %d\n", "Content-Length", content_len);
        data.append(sz);
    }

    data.append("\n");
    if (content_len > 0)
    {
        data.append(m_content);
    }
    data.append("\n");

    return data.c_str();
}
